/*
author:杨文超
email:yangwenchao@keynection.cn
*/
#include <stdio.h>
#include "nvs_flash.h"
#include "esp_log.h"
#include "esp_system.h"
#include "esp_netif.h"
#include "esp_event.h"
#include "esp_sntp.h"
#include "geminai_spiffs.h"
#include "geminai_4g_ap.h"
#include "geminai_gpio.h"

#include "geminai_para.h"
#include "geminai_sdklink_connect.h"
#include "geminai_mqtt_cache.h"
#include "geminai_frig.h"
#include "geminai_CH455.h"
#include "geminai_gpio.h"
#include "geminai_uart.h"
#include "geminai_adc.h"
#include "geminai_led_pwm.h"
#include "geminai_audio.h"
#include "geminai_rs485.h"
#include "geminai_modbus.h"

#include "geminai_get_temp.h"
#include "geminai_para.h"
#include "geminai_selfcheck.h"

#include "geminai_power.h"
#include "geminai_usbh_modem_wifi.h"
#include "geminai_modem_dce_cmd.h"
#include "geminai_usbh_modem_board.h"
#include "geminai_log.h"
#include "geminai_console.h"
#include "geminai_modbus.h"
#include "cmd_oss.h"
#include "geminai_wind_protocol_wrap.h"


#include "display_photo_event.h"
static const char *TAG = "GEMINAI_MAIN";
//EXT_RAM_BSS_ATTR char log_msg[LOG_BUFFER_SIZE];

void app_main(void)
{
    int a;
    GEMINAI_LOGI(TAG, "Startup..");
    GEMINAI_LOGI(TAG, "Free memory: %"PRIu32" bytes", esp_get_free_heap_size());
    GEMINAI_LOGI(TAG, "IDF version: %s", esp_get_idf_version());
    
    esp_err_t ret = geminai_spiffs_init("/spiffs","storage");
    if (ESP_OK != ret) {
        GEMINAI_LOGE(TAG, "spiffs partition initial failed, exit");
        return;
    }
    //Initialize NVS
    initialize_NVS();
    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default() );

    //modbus_task_start();
    geminai_log_init();
    geminai_console_init();


#if 0
    vTaskDelay(pdMS_TO_TICKS(10));
    for (int i = 0; i < 10; i++) {
        snprintf(log_msg, LOG_BUFFER_SIZE, "这是第 %d 条日志", i);
        GEMINAI_LOGI(TAG, "%s", log_msg);
        vTaskDelay(pdMS_TO_TICKS(100));  // 模拟延迟
    }
    while(1)
    {
        //GEMINAI_LOGD("main", "main loop debug");
        GEMINAI_LOGI("main", "main loop info");
        //GEMINAI_LOGW("main", "main loop warn");
        //GEMINAI_LOGE("main", "main loop error");
        vTaskDelay(pdMS_TO_TICKS(10000));
    }
#endif

    adc_init();//启用adc
    boot_check();
    All_gpio_init();//gpio设置初始化
    led_pwm_init();//灯光pwm控制初始化
    initialize_para();//参数初始化

    // Set timezone to China Standard Time
    setenv("TZ", "CST-8", 1);
    tzset();

    modem_ap_task_start();
    CH455_Init();//数码管初始化
    Meter_Init();//电量计
    geminai_i2s_init_std_simplex();
    audio_create_task();
    
    creat_lowpowermode_task();

    selfcheck_task(NULL);

    get_temp_task();
    frig_task(); 
    
    // send_disp_photo_event(30);
    // get_client_sn();
    // char path[100];
    // construct_path(path,100);
    send_disp_photo_event(30);
    create_modbus_task();
    // task_test_wifi_collect();
}

